<template>
 <div class="login_container">
   <div class="nav">
     <div class="header-left">
       <el-button round>神鸡后台</el-button>
     </div>
     <div class="header-right">
       <el-button round>前台页面</el-button>
     </div>
   </div>
  <div class="login_box">
    <!-- 头像区 -->
    <div class="avator_box">
      <img src="../assets/cat.jpg" alt="">
    </div>
    <div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form" label-width="0px">
        <el-form-item prop="loginAcct" class="input_text">
          <el-input v-model="loginForm.loginAcct" name="loginAcct" prefix-icon="el-icon-s-custom" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password"  class="input_text">
          <el-input v-model="loginForm.password" name="password" type="password" prefix-icon="el-icon-s-finance" placeholder="请输入密码"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
         <el-button type="primary" @click="login">登录</el-button>
         <el-button type="info" @click="reset">重置</el-button></el-form-item>
        </el-form>
    </div>
  </div>
 </div>
</template>

<script>
export default {
  data () {
    return {
      // 这是登录表单的数据绑定对象
      loginForm: {
        loginAcct: 'Tom',
        password: '123456'
      },
      loginFormRules: {
        // 验证用户名是否合法
        loginAcct: [
          { required: true, message: '请输入正确用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入正确密码', trigger: 'blur' }
        ]
        // 验证密码是否合法
      }
    }
  },
  methods: {
    // 点击重置按钮，重置表单
    reset () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async vaild => {
        if (!vaild) {
          return null
        } else {
          const res = await this.$http.post('login', this.loginForm)
          var result = res.data
          if (result.result !== 'SUCCESS') {
            return this.$message.error(result.message)
          } else {
            this.$message.success('登陆成功')
            // 把token放入sessionStorage里头，后续需用他验证
            // token只会在浏览器打开生效，关闭将没有
            window.sessionStorage.setItem('token', result.data.token)
            window.sessionStorage.setItem('loginAcct', this.loginForm.loginAcct)
            setTimeout(t => {
              // 延时两秒后跳转到home页面
              this.$router.push({
                path: '/home'
              })
            }, 2000)
          }
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .login_container {
    background-color: #409EFF;
    height: 100%;
  }
  .login_box {
    width: 550px;
    height: 350px;
    background-color: whitesmoke;
    border-radius: 30px;
    box-shadow: 0 0 30px #00007f;
    position: relative;
    left: 50%;
    top: 50%;
    transform:translate(-50%,-50%);
  }
  .avator_box{
    height: 130px;
    width: 130px;
    border: 2px solid whitesmoke;
    border-radius: 75px;
    padding: 10px;
    box-shadow: 0 0 10px;
    position: relative;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: whitesmoke;
    img {
      width: 100%;
      height: 100%;
      border: 1px solid #ccccc9;
      border-radius: 75px;
      background-color: #d3d3d3;
      background-size: cover;
    }
  }
  .btns{
    display: flex;
    justify-content: flex-end;
  }
  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0px 30px;
    // 表示是一个 带边距的box
    box-sizing: border-box;
  }

  .input_text {
    margin-bottom: 50px;
  }
  .nav {
    background-color: #409EFF;
    height: 60px;
  }
  .header-left {
    float: left;
    position: relative;
    top: 40px;
    left: 30px;
  }
  .header-right {
    float: right;
    position: relative;
    top: 40px;
    right: 30px;
  }
</style>
